package com.loongtech.bi.action.bigame;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.action.PageHelper;
import com.loongtech.bi.manager.game.*;
import com.loongtech.bi.manager.game.EconomyGameManager.YuanbaoConsumeData;
import com.loongtech.bi.manager.game.EconomyGameManager.YuanbaoData;
import com.loongtech.bi.manager.game.EconomyGameManager.YuanbaoOutputData;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.NumberUtil;
import com.loongtech.core.util.Time;
import org.datanucleus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 游戏内分析-经济分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/game/economyGame")
public class EconomyGameBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private List<YuanbaoData> listYuanbaoData; //元宝数据列表
	private ChartUIData yuanbaoChart;  // 前端引用的元宝图表数据
	
	private List<YuanbaoOutputData> yuanbaoOutputData;  // 元宝产出途径数据
	private ChartUIData yuanbaoOutputChart;  // 元宝产出途径图表
	
	private List<YuanbaoConsumeData> yuanbaoConsumeData;  // 元宝消耗途径数据
	private ChartUIData yuanbaoConsumeChart;  // 元宝消耗途径图表
    private ChartUIData yuanbaoConsumeChart1;  // 元宝消耗途径图表

    private String queryCurType;

    private List<EconomyGameManager.YuanbaoSumData> yuanbaoSumDatas;
    private ChartUIData yuanbaoSumChart;

    private List<YuanbaoConsumeData> shangchengConsumeDatas;
    private ChartUIData shangchengConsumeChart;
    private ChartUIData shangchengConsumeChart1;

	@Autowired
	private EconomyGameManager economyGameManager;
	@Autowired
    private MallStatisticsAllManager mallStatisticsAllManager;
	@Autowired
    private MallStatisticsAllNoParamManager mallStatisticsAllNoParamManager;

    private Map<String, String> yuabaoItmes = GameParamConfigManager.getItems(CommonGameParam.ECONOMY_YUAN_BAO_ID);
	private Map<String, String> yuabaoItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.ECONOMY_YUAN_BAO_ID);
    private Map<String, String> rmbItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.RMB_REASON);  // 元宝产出和消耗途径反转

    @RequestMapping(value = "initParam.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);

            yuabaoItmes.remove("全部经济统计元宝ID");
            yuabaoItmesRevise.remove("-99999");
            for (String s : yuabaoItmes.values()) {
                queryCurType = s;
                break;
            }
            result.put("yuabaoItmes", yuabaoItmes);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, @RequestParam String curType, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            String shangchengReason = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.SHANGCHENG_REASON).keySet().toArray()[1];
            if (StringUtils.isEmpty(shangchengReason) || GameParamConfigManager.ALL_FLAT.equals(shangchengReason)) {
                result.put("res", "1");
                result.put("msg", "没有配置 64 消耗原因是商城 的值 !");
                printToJson(response, result, callback);
                return;
            }

            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);
            queryPlatform = platform;
            queryChannel = channel;
            queryServer = server;
            queryCurType = curType;

            //元宝数据列表
            listYuanbaoData = economyGameManager.getListYuanbaoData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,queryCurType);

            if (listYuanbaoData != null && listYuanbaoData.size() > 0) {
                yuanbaoChart = new ChartUIData();
                yuanbaoChart.setJsonType(ChartType.SPLINE.type);
                yuanbaoChart.setJsonStr(new ChartTool(listYuanbaoData, YuanbaoData.class, yuanbaoChart.getSelectItem()).loadData(ChartType.SPLINE.type));
                yuanbaoChart.setAllItmes(ChartTool.getAllItems(YuanbaoData.class));
            } else {
                yuanbaoChart = null;
            }

            //产出数据
            yuanbaoOutputData = economyGameManager.getListYuanbaoOutputData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,queryCurType);
            if (yuanbaoOutputData != null && yuanbaoOutputData.size() > 0) {
                yuanbaoOutputChart = new ChartUIData();
                yuanbaoOutputChart.setJsonType(ChartType.BAR2.type);
                yuanbaoOutputChart.setJsonStr(new ChartTool(yuanbaoOutputData, YuanbaoOutputData.class, yuanbaoOutputChart.getSelectItem()).loadData(ChartType.BAR.type));
                yuanbaoOutputChart.setAllItmes(ChartTool.getAllItems(YuanbaoOutputData.class));
            } else {
                yuanbaoOutputChart = null;
            }

            //消耗数据
            yuanbaoConsumeData = economyGameManager.getListYuanbaoConsumeData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, queryCurType);
            if (yuanbaoConsumeData != null && yuanbaoConsumeData.size() > 0) {
                yuanbaoConsumeChart = new ChartUIData();
                yuanbaoConsumeChart.setJsonType(ChartType.BAR2.type);
                yuanbaoConsumeChart.setAllItmes(ChartTool.getAllItems(YuanbaoConsumeData.class));
                Collections.sort(yuanbaoConsumeData, economyGameManager.new YuanbaoConsumeData().comparatorConsumeCount);
                List<YuanbaoConsumeData> ll = yuanbaoConsumeData.size() > 20 ? yuanbaoConsumeData.subList(0, 20) : yuanbaoConsumeData;
                Collections.reverse(ll);
                yuanbaoConsumeChart.setJsonStr(new ChartTool(ll, YuanbaoConsumeData.class, "consumeCount").loadData(ChartType.BAR.type));

                yuanbaoConsumeChart1 = new ChartUIData();
                yuanbaoConsumeChart1.setJsonType(ChartType.BAR2.type);
                yuanbaoConsumeChart1.setAllItmes(ChartTool.getAllItems(YuanbaoConsumeData.class));
                Collections.sort(yuanbaoConsumeData, economyGameManager.new YuanbaoConsumeData().comparatorConsumeUser);
                List<YuanbaoConsumeData> ll1 = yuanbaoConsumeData.size() > 20 ? yuanbaoConsumeData.subList(0, 20) : yuanbaoConsumeData;
                Collections.reverse(ll1);
                yuanbaoConsumeChart1.setJsonStr(new ChartTool(ll1, YuanbaoConsumeData.class, "consumeUser").loadData(ChartType.BAR.type));
            } else {
                yuanbaoConsumeChart = null;
                yuanbaoConsumeChart1 = null;
            }

            yuanbaoSumDatas = economyGameManager.getYuanbaoSumDatas(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,queryCurType);
            if (yuanbaoSumDatas != null && yuanbaoSumDatas.size() > 0) {
                yuanbaoSumChart = new ChartUIData();
                yuanbaoSumChart.setJsonStr(ChartType.SPLINE.type);
                yuanbaoSumChart.setJsonStr(new ChartTool(yuanbaoSumDatas, EconomyGameManager.YuanbaoSumData.class, "").loadData(yuanbaoSumChart.getJsonType()));
            } else {
                yuanbaoSumChart = null;
            }

            Map<String, YuanbaoConsumeData> tmp = new LinkedHashMap<>();

            List<Object[]> mallStatisticsAllList = mallStatisticsAllManager.getMallStatisticsAllByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, "(" + queryCurType + ")", shangchengReason);
            for(Object[] o : mallStatisticsAllList) {
                String way = NumberUtil.parseLong(o[2]) + "";
                YuanbaoConsumeData e = tmp.get(way);
                if (e == null) {
                    e = economyGameManager.new YuanbaoConsumeData();
                    e.setConsumeWay(way);
                    e.setConsumeWayStr(GameParamConfigManager.getItemsRevise(CommonGameParam.ITEM_TYPE).get(way) == null ? way : GameParamConfigManager.getItemsRevise(CommonGameParam.ITEM_TYPE).get(way));

                    tmp.put(way, e);
                }

                e.setConsumeCount(e.getConsumeCount() - NumberUtil.parseLong(o[3]));
                e.setConsumeUser(e.getConsumeUser() + NumberUtil.parseInt(o[5]));
            }
            if (tmp.size() > 0) {
                shangchengConsumeDatas = new ArrayList<>();
                shangchengConsumeDatas.addAll(tmp.values());

                List<Object[]> list = mallStatisticsAllNoParamManager.getMallStatisticsAllByAllKind(Time.getDateStr(dateStart), Time.getDateStr(dateEnd), queryPlatform, queryChannel, queryServer, "(" + queryCurType + ")", shangchengReason);
                long allConsume = 0;
                long allUser = 0;
                for(Object[] o1 : list) {
                    allConsume = NumberUtil.parseLong(o1[0]);
                    allUser = NumberUtil.parseLong(o1[1]);
                }
                for (YuanbaoConsumeData e : shangchengConsumeDatas) {
                    e.setConsumeCountRadio(allConsume == 0 ? 0 : (float)(e.getConsumeCount() * -1.0 / allConsume));
                    e.setConsumeUserRadio(allUser == 0 ? 0 : (float)(e.getConsumeUser() * 1.0 / allUser));
                }

                shangchengConsumeChart = new ChartUIData();
                shangchengConsumeChart.setJsonType(ChartType.BAR2.type);
                shangchengConsumeChart.setAllItmes(ChartTool.getAllItems(YuanbaoConsumeData.class));
                Collections.sort(shangchengConsumeDatas, economyGameManager.new YuanbaoConsumeData().comparatorConsumeCount);
                List<YuanbaoConsumeData> ll = shangchengConsumeDatas.size() > 20 ? shangchengConsumeDatas.subList(0, 20) : shangchengConsumeDatas;
                Collections.reverse(ll);
                shangchengConsumeChart.setJsonStr(new ChartTool(ll, YuanbaoConsumeData.class, "consumeCount").loadData(ChartType.BAR.type));

                shangchengConsumeChart1 = new ChartUIData();
                shangchengConsumeChart1.setJsonType(ChartType.BAR2.type);
                shangchengConsumeChart1.setAllItmes(ChartTool.getAllItems(YuanbaoConsumeData.class));
                Collections.sort(shangchengConsumeDatas, economyGameManager.new YuanbaoConsumeData().comparatorConsumeUser);
                List<YuanbaoConsumeData> ll1 = shangchengConsumeDatas.size() > 20 ? shangchengConsumeDatas.subList(0, 20) : shangchengConsumeDatas;
                Collections.reverse(ll1);
                shangchengConsumeChart1.setJsonStr(new ChartTool(ll1, YuanbaoConsumeData.class, "consumeUser").loadData(ChartType.BAR.type));
            } else {
                shangchengConsumeDatas = null;
                shangchengConsumeChart = null;
                shangchengConsumeChart1 = null;
            }

            result.put("res", "0");
            result.put("yuanbaoSumDatas", yuanbaoSumDatas);
            result.put("yuanbaoSumChart", yuanbaoSumChart);
            result.put("yuanbaoConsumeData", yuanbaoConsumeData);
            result.put("yuanbaoConsumeChart", yuanbaoConsumeChart);
            result.put("yuanbaoConsumeChart1", yuanbaoConsumeChart1);
            result.put("shangchengConsumeDatas", shangchengConsumeDatas);
            result.put("shangchengConsumeChart", shangchengConsumeChart);
            result.put("shangchengConsumeChart1", shangchengConsumeChart1);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}

	//获得百分比
	public String getDouble2Percent(Double d) {
		return PageHelper.double2Percent(d);
	}
	
	// 响应前端曲线图改变选择项事件
	public void handleYuanbaoChange() throws Exception {
		yuanbaoChart.setJsonStr(new ChartTool(listYuanbaoData, YuanbaoData.class, yuanbaoChart.getSelectItem()).loadData(ChartType.SPLINE.type));
	}
	
	// 响应前端曲线图改变选择项事件
	public void handleYuanbaoOutputChange() throws Exception {
		yuanbaoOutputChart.setJsonStr(new ChartTool(yuanbaoOutputData, YuanbaoOutputData.class, yuanbaoOutputChart.getSelectItem()).loadData(ChartType.BAR.type));
	}
	
	// 响应前端曲线图改变选择项事件
    @RequestMapping(value = "updateConsumeType.do", method = {RequestMethod.GET, RequestMethod.POST})
	public void updateConsumeType(HttpServletRequest request, HttpServletResponse response, @RequestParam String consumeType, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            if ("consumeUser".equals(consumeType)) {
                Collections.sort(yuanbaoConsumeData, economyGameManager.new YuanbaoConsumeData().comparatorConsumeUser);
            } else {
                Collections.sort(yuanbaoConsumeData, economyGameManager.new YuanbaoConsumeData().comparatorConsumeCount);
            }
            List<YuanbaoConsumeData> ll = yuanbaoConsumeData.size() > 20 ? yuanbaoConsumeData.subList(0, 20) : yuanbaoConsumeData;
            Collections.reverse(ll);
            yuanbaoConsumeChart.setJsonStr(new ChartTool(ll, YuanbaoConsumeData.class, consumeType).loadData(ChartType.BAR.type));

            result.put("res", "0");
            result.put("chart", yuanbaoConsumeChart);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}

    @RequestMapping(value = "updateConsumeType2.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateConsumeType2(HttpServletRequest request, HttpServletResponse response, @RequestParam String consumeType, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            if ("consumeUser".equals(consumeType)) {
                Collections.sort(shangchengConsumeDatas, economyGameManager.new YuanbaoConsumeData().comparatorConsumeUser);
            } else {
                Collections.sort(shangchengConsumeDatas, economyGameManager.new YuanbaoConsumeData().comparatorConsumeCount);
            }

            List<YuanbaoConsumeData> ll = shangchengConsumeDatas.size() > 20 ? shangchengConsumeDatas.subList(0, 20) : shangchengConsumeDatas;
            Collections.reverse(ll);
            shangchengConsumeChart.setJsonStr(new ChartTool(ll, YuanbaoConsumeData.class, consumeType).loadData(ChartType.BAR.type));

            result.put("res", "0");
            result.put("chart", shangchengConsumeChart);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    @RequestMapping(value = "updateServer.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateServer(HttpServletRequest request, HttpServletResponse response, String server, String callback) {
        try {
            queryServer = server;
            selectedServer();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updateChannel.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateChannel(HttpServletRequest request, HttpServletResponse response, String channel, String callback) {
        try {
            queryChannel = channel;
            updateServerItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updatePlatform.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updatePlatform(HttpServletRequest request, HttpServletResponse response, String platform, String callback) {
        try {
            queryPlatform = platform;
            updateChannelItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @Override
    public void search() {

    }

    @Override
	protected String csvContent() {
		return "经济分布";
	}

	@Override
    protected String csvFileName() {
	    return "经济分布";
    }

	public List<YuanbaoData> getListYuanbaoData() {
		return listYuanbaoData;
	}

	public void setListYuanbaoData(List<YuanbaoData> listYuanbaoData) {
		this.listYuanbaoData = listYuanbaoData;
	}

	public ChartUIData getYuanbaoChart() {
		return yuanbaoChart;
	}

	public void setYuanbaoChart(ChartUIData yuanbaoChart) {
		this.yuanbaoChart = yuanbaoChart;
	}

	public List<YuanbaoOutputData> getYuanbaoOutputData() {
		return yuanbaoOutputData;
	}

	public void setYuanbaoOutputData(List<YuanbaoOutputData> yuanbaoOutputData) {
		this.yuanbaoOutputData = yuanbaoOutputData;
	}

	public ChartUIData getYuanbaoOutputChart() {
		return yuanbaoOutputChart;
	}

	public void setYuanbaoOutputChart(ChartUIData yuanbaoOutputChart) {
		this.yuanbaoOutputChart = yuanbaoOutputChart;
	}

	public List<YuanbaoConsumeData> getYuanbaoConsumeData() {
		return yuanbaoConsumeData;
	}

	public void setYuanbaoConsumeData(List<YuanbaoConsumeData> yuanbaoConsumeData) {
		this.yuanbaoConsumeData = yuanbaoConsumeData;
	}

	public ChartUIData getYuanbaoConsumeChart() {
		return yuanbaoConsumeChart;
	}

	public void setYuanbaoConsumeChart(ChartUIData yuanbaoConsumeChart) {
		this.yuanbaoConsumeChart = yuanbaoConsumeChart;
	}

    public String getQueryCurType() {
        return queryCurType;
    }

    public void setQueryCurType(String queryCurType) {
        this.queryCurType = queryCurType;
    }

    public Map<String, String> getYuabaoItmesRevise() {
        return yuabaoItmesRevise;
    }

    public void setYuabaoItmesRevise(Map<String, String> yuabaoItmesRevise) {
        this.yuabaoItmesRevise = yuabaoItmesRevise;
    }

    public Map<String, String> getRmbItmesRevise() {
        return rmbItmesRevise;
    }

    public void setRmbItmesRevise(Map<String, String> rmbItmesRevise) {
        this.rmbItmesRevise = rmbItmesRevise;
    }

    public Map<String, String> getYuabaoItmes() {
        return yuabaoItmes;
    }

    public void setYuabaoItmes(Map<String, String> yuabaoItmes) {
        this.yuabaoItmes = yuabaoItmes;
    }

    public List<EconomyGameManager.YuanbaoSumData> getYuanbaoSumDatas() {
        return yuanbaoSumDatas;
    }

    public void setYuanbaoSumDatas(List<EconomyGameManager.YuanbaoSumData> yuanbaoSumDatas) {
        this.yuanbaoSumDatas = yuanbaoSumDatas;
    }

    public ChartUIData getYuanbaoSumChart() {
        return yuanbaoSumChart;
    }

    public void setYuanbaoSumChart(ChartUIData yuanbaoSumChart) {
        this.yuanbaoSumChart = yuanbaoSumChart;
    }

    public List<YuanbaoConsumeData> getShangchengConsumeDatas() {
        return shangchengConsumeDatas;
    }

    public void setShangchengConsumeDatas(List<YuanbaoConsumeData> shangchengConsumeDatas) {
        this.shangchengConsumeDatas = shangchengConsumeDatas;
    }

    public ChartUIData getShangchengConsumeChart() {
        return shangchengConsumeChart;
    }

    public void setShangchengConsumeChart(ChartUIData shangchengConsumeChart) {
        this.shangchengConsumeChart = shangchengConsumeChart;
    }

    public ChartUIData getYuanbaoConsumeChart1() {
        return yuanbaoConsumeChart1;
    }

    public void setYuanbaoConsumeChart1(ChartUIData yuanbaoConsumeChart1) {
        this.yuanbaoConsumeChart1 = yuanbaoConsumeChart1;
    }

    public ChartUIData getShangchengConsumeChart1() {
        return shangchengConsumeChart1;
    }

    public void setShangchengConsumeChart1(ChartUIData shangchengConsumeChart1) {
        this.shangchengConsumeChart1 = shangchengConsumeChart1;
    }
}
